package com.shelpe.services.sinbad.service.simbaproxy;

import java.util.Date;
import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
//, fallback = ImageClientFallback.class
@FeignClient(name = "${feign.simba.name}", url = "${feign.simba.url}")
public interface ImageClient {
	
	@RequestMapping(method = RequestMethod.POST, value = "/simba/taobao.item.img.upload", consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
	AddProductImgResponse addImg(@RequestHeader("tenantID") long tenantID,
			@RequestParam("num_iid") long productIID,
			@RequestBody MultipartFile image);
	
	@RequestMapping(method = RequestMethod.POST, value = "/simba/taobao.item.img.upload", consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
	AddProductImgResponse updateImg(@RequestHeader("tenantID") long tenantID,
			@RequestParam("num_iid") long productID,
			@RequestParam("id") long imgID,
			@RequestBody MultipartFile image);
	
	@Data
	@EqualsAndHashCode(callSuper=true)
	public static class AddProductImgResponse extends SimbaProxyBaseDto {
		@JsonProperty("item_img_upload_response")
		private AddProductImgRes res;
	}

	@Data
	public static class AddProductImgRes {
		@JsonProperty("item_img")
		private ItemImg itemImg;
	}
	
	@Data
	public static class ItemImg	{
		@JsonProperty("id")
		private long imgId;
		@JsonProperty("url")
		private String imgUrl;		
		@JsonProperty("created")
		@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
		private Date created;
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value = "/simba/taobao.picture.upload", consumes=MediaType.MULTIPART_FORM_DATA_VALUE)
	AddPictureResponse addPicture(@RequestHeader("tenantID") long tenantID,
			@RequestParam("picture_category_id") long pictureCategoryID,
			@RequestParam("image_input_title") String title,
			@RequestBody MultipartFile img);
	@Data
	@EqualsAndHashCode(callSuper=true)
	public static class AddPictureResponse extends SimbaProxyBaseDto {
		@JsonProperty("picture_upload_response")
		private PictureData res;
	}

	@Data
	public static class PictureData {
		@JsonProperty("picture")
		private Picture picture;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/simba/taobao.picture.pictures.get")
	GetPictureResponse listPicture(@RequestHeader("tenantID") long tenantID,
			@RequestParam("page_size") int pageSize,
			@RequestParam("current_page") int pageNumber);
	
	@Data
	@EqualsAndHashCode(callSuper=true)
	public static class GetPictureResponse extends SimbaProxyBaseDto {
		@JsonProperty("picture_pictures_get_response")
		private PicturesData res;
	}
	
	@Data
	public static class PicturesData {
		@JsonProperty("pictures")
		private PictureList list;
	}
	
	@Data
	public static class PictureList {
		@JsonProperty("picture")
		private List<Picture> pictures;
	}
	
	@Data
	public static class Picture{
		@JsonProperty("picture_id")
		private long pictureID;
		@JsonProperty("picture_path")
		private String pictureUrl;
		
		private String title;
		@JsonProperty("sizes")
		private long pictureSize;
		
		@JsonProperty("created")
		@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
		private Date created;
	}
	
	@Service
	@Slf4j
	public static class ImageClientFallback implements ImageClient{
		
		@Override
		public AddProductImgResponse addImg(long tenantID, long productIID, MultipartFile image) {
			// TODO Auto-generated method stub
			log.error("fallback addImg: {},{},{}", tenantID, productIID, image);
			return new AddProductImgResponse();
		}

		@Override
		public AddProductImgResponse updateImg(long tenantID, long productID, long imgID, MultipartFile image) {
			// TODO Auto-generated method stub
			log.error("fallback updateImg: {},{},{}", tenantID, productID,imgID, image);
			return new AddProductImgResponse();
		}

		@Override
		public AddPictureResponse addPicture(long tenantID, long pictureCategoryID, String title, MultipartFile img) {
			// TODO Auto-generated method stub
			log.error("fallback addImg: {},{},{},{}", tenantID, pictureCategoryID, title,img);
			return new AddPictureResponse();
		}

		@Override
		public GetPictureResponse listPicture(long tenantID, int pageSize, int pageNumber) {
			// TODO Auto-generated method stub
			log.error("fallback addImg: {}", tenantID);
			return new GetPictureResponse();
		}
		
	}
}
